Make A Dynamic Page Accordion Menu With jQuery

Under: Wordpress,jQuery 32 comments

There are many ways to add an flexible accordion menu to your web page. Most of them use div or paragraph but in this post I’m going to use unordered list and jQuery toggle to make an accordion menu.The most important information here is how to toggle list and how to hide/show the related page to viewer. I explained here specifically on dynamic page display with WordPress, you can always take the same theory and apply in other situations.

    Features:

  1. Work in WordPress
  2. Accordion Slide
  3. Show only top level page list when visiting top level pages
  4. Show current page’s child page
  5. Show allpages when visiting child page

WordPress Code to display page list

I got this code from wordpress.org

<?php
  if($post->post_parent)
  $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
  else
  $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
  if ($children) { ?>
  <ul>
  <?php echo $children; ?>
  </ul>
  <?php } ?>

The code displays all pages. This is the html code generated by WordPress

<div id="page-list"><ul>
<li class="page_item page-item-1611"><a href="http://design-notes.info/archives/" title="Archives">Archives</a></li>
<li class="page_item page-item-742"><a href="http://design-notes.info/blogroll/" title="Blogroll">Blogroll</a>
<ul>
<li class="page_item page-item-1683"><a href="http://design-notes.info/blogroll/this-is-a-demo-page/" title="This is a demo page">This is a demo page</a></li>
 
</ul>
</li>
<li class="page_item page-item-1096"><a href="http://design-notes.info/linking/" title="linking">linking</a></li>
<li class="page_item page-item-113"><a href="http://design-notes.info/about/" title="About">About</a></li>
</ul>
</div>

Javascripts

Now what we need to do is to create accordion with jQuery’s toggle function and make it dynamically display the page you want your reader to see. WordPress’s “current_page_item” class is very useful here. I used it on line 5 to list parent pages and again on line 9 to show its child pages.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$(document).ready(function()
{//hide child page
$('#page-list ul li.page_item ul').hide();
//when navigate to a child page show all pages
$('#page-list ul li.current_page_item').parent("ul").show();
//show page list when toggle
$('#page-list ul li.active ul').show();
//show cerrent page's child page
$('#page-list ul li.current_page_item ul').show();
 
	 $('#page-list ul li').click(function() { 
$(this).addClass("active");
                    $(this).children('ul').slideToggle("slow"); 
            });
 
 
});

CSS

#page-list {
float:left;
clear:left;
margin:20px 0 0 10px;
display:block;
position:relative;
}
#page-list ul {
	list-style:none;
	margin:0;
	}
#page-list ul li{
padding:0 10px 0 10px;
margin:2px 0;
display:block;
cursor:pointer;
background-color:#ccc;
}
#page-list ul li ul li{
padding:0;
margin:0;
position:relative;
 
}
#page-list ul li ul li a{
	}
#page-list ul li a{
padding:3px 5px;
display:block;
width:150px;
background-color:#f4f4f4;
}

Conclusion

I first displayed all pages then hide or show them dynamically with jQuery. Its quite handy than using php alone. You can check out the demo page list on the sidebar by navigating to pages. I made a couple of demo pages for you too.